<!--
 * @Descripttion: (工位配置表/workstation_config)
 * @Author: (admin)
 * @Date: (2024-09-29)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item label="产线名称" prop="productionLineCode">
        <el-input v-model="queryParams.productionLineCode" placeholder="请输入产线名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="服务器" prop="hmiCode">
        <el-input v-model="queryParams.hmiCode" placeholder="请输入服务器名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="工站名称" prop="workstationCode">
        <el-input v-model="queryParams.workstationCode" placeholder="请输入工站名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['workstationconfig:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="id" label="Id" align="center" v-if="columns.showColumn('id')"/>
      <el-table-column prop="productionLineCode" label="产线代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('productionLineCode')"/>
      <el-table-column prop="hmiCode" label="上位机代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('hmiCode')"/>
      <el-table-column prop="workstationCode" label="工位代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('workstationCode')"/>
      <el-table-column prop="workstationName" label="工位名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('workstationName')"/>
      <!-- <el-table-column prop="plcEnabled" label="PLC启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('plcEnabled')"/> -->
      <el-table-column label="PLC" align="center" v-if="columns.showColumn('plcEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.plcEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="scannerEnabled" label="扫码枪" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('scannerEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.scannerEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="ccdEnabled" label="CCD" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('ccdEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.ccdEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="printerEnabled" label="打印机" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('printerEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.printerEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="markingMachineEnabled" label="打标机" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('markingMachineEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.markingMachineEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="materialVerificationEnabled" label="物料校验" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('materialVerificationEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.materialVerificationEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="toleranceVerificationEnabled" label="检测公差" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('toleranceVerificationEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.toleranceVerificationEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="stationVerifyEnabled" label="工位验证" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stationVerifyEnabled')">
            <template #default="scope">
              <el-switch
                v-model="scope.row.stationVerifyEnabled"
                :active-value="1"
                :inactive-value="0"
                active-text="是"
                inactive-text="否"
                inline-prompt
                @change="handleStatusOfChange(scope.row)"
                ></el-switch>
            </template>
          </el-table-column>
      <!-- <el-table-column prop="scannerEnabled" label="扫码枪启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('scannerEnabled')"/>
      <el-table-column prop="ccdEnabled" label="CCD启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('ccdEnabled')"/>
      <el-table-column prop="printerEnabled" label="打印机启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('printerEnabled')"/>
      <el-table-column prop="markingMachineEnabled" label="打标机启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('markingMachineEnabled')"/>
      <el-table-column prop="materialVerificationEnabled" label="物料校验启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('materialVerificationEnabled')"/>
      <el-table-column prop="toleranceVerificationEnabled" label="公差带校验启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('toleranceVerificationEnabled')"/>
      <el-table-column prop="stationVerifyEnabled" label="工位验证启用" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stationVerifyEnabled')"/> -->
      <!-- <el-table-column prop="isEnabled" label="是否启用（软删除）" align="center" v-if="columns.showColumn('isEnabled')">
        <template #default="scope">
          <dict-tag :options=" options.isEnabledOptions" :value="scope.row.isEnabled"  />
        </template>
      </el-table-column>
      <el-table-column prop="creationTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('creationTime')"/>
      <el-table-column prop="creatorName" label="创建人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('creatorName')"/> -->
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['workstationconfig:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['workstationconfig:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <!-- 添加或修改工位配置表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="产线代号" prop="productionLineCode">
              <el-input v-model="form.productionLineCode" placeholder="请输入产线代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="上位机代号" prop="hmiCode">
              <el-input v-model="form.hmiCode" placeholder="请输入上位机代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位代号" prop="workstationCode">
              <el-input v-model="form.workstationCode" placeholder="请输入工位代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位名称" prop="workstationName">
              <el-input v-model="form.workstationName" placeholder="请输入工位名称" />
            </el-form-item>
          </el-col>
          
          <el-col :lg="8">
            <el-form-item label="PLC" prop="plcEnabled">
              <el-radio-group v-model="form.plcEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="8">
            <el-form-item label="扫码枪" prop="scannerEnabled">
              <el-radio-group v-model="form.scannerEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="CCD" prop="ccdEnabled">
              <el-radio-group v-model="form.ccdEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="打印机" prop="printerEnabled">
              <el-radio-group v-model="form.printerEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="打标机" prop="markingMachineEnabled">
              <el-radio-group v-model="form.markingMachineEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="物料校验" prop="materialVerificationEnabled">
              <el-radio-group v-model="form.materialVerificationEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="检测校验" prop="toleranceVerificationEnabled">
              <el-radio-group v-model="form.toleranceVerificationEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="8">
            <el-form-item label="工位验证" prop="stationVerifyEnabled">
              <el-radio-group v-model="form.stationVerifyEnabled">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :value="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="workstationconfig">
import { listWorkstationConfig,
 addWorkstationConfig, delWorkstationConfig, 
 updateWorkstationConfig,getWorkstationConfig, 
 } 
from '@/api/business/workstationconfig.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const statusOptions = ref([])
const queryParams = reactive({
  productionLineCode:"",
  hmiCode:"",
  workstationCode:"",
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: false, prop: 'id', label: '序号' },
  { visible: true, prop: 'productionLineCode', label: '产线代号' },
  { visible: true, prop: 'hmiCode', label: '上位机代号' },
  { visible: true, prop: 'workstationCode', label: '工位代号' },
  { visible: true, prop: 'workstationName', label: '工位名称' },
  { visible: true, prop: 'plcEnabled', label: 'PLC' },
  { visible: true, prop: 'scannerEnabled', label: '扫码枪' },
  { visible: true, prop: 'ccdEnabled', label: 'CCD' },
  { visible: true, prop: 'printerEnabled', label: '打印机' },
  { visible: true, prop: 'markingMachineEnabled', label: '打标机' },
  { visible: true, prop: 'materialVerificationEnabled', label: '物料校验' },
  { visible: true, prop: 'toleranceVerificationEnabled', label: '检测公差' },
  { visible: true, prop: 'stationVerifyEnabled', label: '工位验证' },
  { visible: false, prop: 'isEnabled', label: '是否启用（软删除）' },
  { visible: false, prop: 'creationTime', label: '创建时间' },
  { visible: false, prop: 'creatorName', label: '创建人' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]

proxy.getDicts('business_normal_disable').then((response) => {
  statusOptions.value = response.data
})
function getList(){
  loading.value = true
  listWorkstationConfig(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      
      dataList.value = data.result
      console.log(dataList.value);

      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    id: [{ required: true, message: "Id不能为空", trigger: "blur"    , type: "number"  }],
  },
  options: {
    // 是否启用（软删除） 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
isEnabledOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    productionLineCode: null,
    hmiCode: null,
    workstationCode: null,
    workstationName: null,
    plcEnabled: null,
    scannerEnabled: null,
    ccdEnabled: null,
    printerEnabled: null,
    markingMachineEnabled: null,
    materialVerificationEnabled: null,
    toleranceVerificationEnabled: null,
    stationVerifyEnabled: null,
    isEnabled: null,
    creationTime: null,
    creatorName: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加工位配置表'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getWorkstationConfig(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改工位配置表'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

/** 状态修改  */
function handleStatusOfChange(row) {
  console.log(row);
  proxy
    .$confirm('确认要执行当前操作吗?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(function () {
      return updateWorkstationConfig(row)
    })
    .then(() => {
      proxy.$modal.msgSuccess('执行成功！')
    })
}
// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateWorkstationConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWorkstationConfig(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delWorkstationConfig(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}





handleQuery()
</script>